package common

import (
	"DmsRocketMQ/common"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"github.com/pkg/errors"
)

func GetProducer(config common.Config) (rocketmq.Producer, error) {
	p, err := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver(config.Host)),
		producer.WithRetry(2),
	)
	if err != nil {
		return nil, errors.Wrap(err, "ERR: fail to init a producer")
	}
	err = p.Start()
	if err != nil {
		return nil, errors.Wrap(err, "ERR: fail to start producer")
	}
	return p, nil
}
